package LatexForCorrelations;

import java.io.BufferedWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import Analysis.MetaCorrelationsManagersListClass;
import Global.ConstantVariable;
import SyntheticData.syntheticDataGeneratorClass;
import Utility.WorkerUtilityClass;

public class LatexUtilityForSyntheticMetaCorrelationsResults {

	public static class syntheticMetaCorrelationsTableCreatorClass
	{
		public static void printLatexSynMetaCorrelationsForCOTableHeaderFirstPartToTheFile(BufferedWriter out, int numberOfSharedElements)
		{

			try
			{
				out.write("\\begin{table}%[ht])");
				out.write("\n");
				out.write("\\centering");
				out.write("\n");
				out.write("\\caption{Synthetic Datasets Meta Correlations(CO) Results (ms="+numberOfSharedElements+")}");
				out.write("\n");
				out.write("\\begin{tabular}{|c|c|c|c|c|}");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");
				out.write("D. No & c(CO)& h(CO) & A(CO)& a(CO) \\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		public static void addRecordToTheSynMetaCorrelationsCOTableForTheSyntheticDatasetsGeneratedWithTheSameParameters(MetaCorrelationsManagersListClass metaCorrelationsManagersList, int numberOfSharedElements)
		{
			try
			{
				BufferedWriter out;
				
				out = syntheticDataGeneratorClass.outForSyntheticMetaCorrelationsCOResultsFile;
				
				printLatexSynMetaCorrelationsForCOTableHeaderFirstPartToTheFile(out, numberOfSharedElements);
				
				NumberFormat formatter;
				
				NumberFormat formatterForErr;
				
				formatter = new DecimalFormat(ConstantVariable.Format_ConstantVariables.numberFormatToBeUsedForCorrelationsLatexTable);
				formatterForErr = new DecimalFormat(ConstantVariable.Format_ConstantVariables.numberFormatToBeUsedForCorrelationsLatexTable);
								
				//double cor_kCO_hCO = CorrelationCalculatorClass.calculatePearsonCorrelation(graph.getPropertiesOfTheGraphWithRespectToEdgeType().prepareForCorrelationCalculation((degreeDistributionRelatedPropsCO.getDegreeListOfNodes()),graph.getPropertiesOfTheGraphWithRespectToEdgeType().prepareForCorrelationCalculation( homophilyRelatedPropsCO.getHomophilyListOfTheNodes())));
				
				
				out.write("k(CO) &" + formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_kCO_cCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_kCO_cCOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_kCO_hCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_kCO_hCOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_kCO_alphaCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_kCO_alphaCOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_kCO_accuracyCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_kCO_accuracyCOListArray))+"\\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");
				
				out.write("c(CO) & - & " + formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_cCO_hCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_cCO_hCOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_cCO_alphaCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_cCO_alphaCOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_cCO_accuracyCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_cCO_accuracyCOListArray))+"\\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");
				
				out.write("h(CO) & - & - &" + formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_hCO_alphaCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_hCO_alphaCOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_hCO_accuracyCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_hCO_accuracyCOListArray))+"\\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");
				
				out.write("A(CO) & - & - & - &" + formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_alphaCO_accuracyCOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_alphaCO_accuracyCOListArray))+"\\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");
				
				printLatexSynMetaCorrelationsForCOTableHeaderEndPartToTheFile(out, numberOfSharedElements);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		public static void printLatexSynMetaCorrelationsForCOTableHeaderEndPartToTheFile(BufferedWriter out, int numberOfSharedElements)
		{
			/*BufferedWriter out;

		FileWriter fstream = new FileWriter(fileAdditionalProperties);
	    BufferedWriter out = new BufferedWriter(fstream);*/

			try
			{
				out.write("\\end{tabular}");
				out.write("\n");			
				out.write("\\label{table:");				
				out.write("ContentOnlyMetaCorrelations_ms_"+ numberOfSharedElements +"}");
				out.write("\n");
				out.write("\\end{table}");
				out.write("\n");
				out.write("\n");
				out.write("\n");
				out.write("\n");
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		
		/*******************************************************************************/

		public static void printLatexSynMetaCorrelationsForLOTableHeaderFirstPartToTheFile(BufferedWriter out, int numberOfSharedElements)
		{

			try
			{
				out.write("\\begin{table}%[ht])");
				out.write("\n");
				out.write("\\centering");
				out.write("\n");
				out.write("\\caption{Synthetic Datasets Meta Correlations(LO) Results (ms="+numberOfSharedElements+")}");
				out.write("\n");
				out.write("\\begin{tabular}{|c|c|c|c|c|}");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");
				out.write("D. No & c(LO)& h(LO) & A(LO)& a(LO) \\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}

		public static void addRecordToTheSynMetaCorrelationsLOTableForTheSyntheticDatasetsGeneratedWithTheSameParameters(MetaCorrelationsManagersListClass metaCorrelationsManagersList, int numberOfSharedElements)
		{
			try
			{
				BufferedWriter out;

				out = syntheticDataGeneratorClass.outForSyntheticMetaCorrelationsLOResultsFile;
				
				printLatexSynMetaCorrelationsForLOTableHeaderFirstPartToTheFile(out, numberOfSharedElements);

				NumberFormat formatter;

				NumberFormat formatterForErr;

				formatter = new DecimalFormat(ConstantVariable.Format_ConstantVariables.numberFormatToBeUsedForCorrelationsLatexTable);
				formatterForErr = new DecimalFormat(ConstantVariable.Format_ConstantVariables.numberFormatToBeUsedForCorrelationsLatexTable);

				//double cor_kLO_hLO = CorrelationCalculatorClass.calculatePearsonCorrelation(graph.getPropertiesOfTheGraphWithRespectToEdgeType().prepareForCorrelationCalculation((degreeDistributionRelatedPropsLO.getDegreeListOfNodes()),graph.getPropertiesOfTheGraphWithRespectToEdgeType().prepareForCorrelationCalculation( homophilyRelatedPropsLO.getHomophilyListOfTheNodes())));


				out.write("k(LO) &" + formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_kLO_cLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_kLO_cLOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_kLO_hLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_kLO_hLOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_kLO_alphaLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_kLO_alphaLOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_kLO_accuracyLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_kLO_accuracyLOListArray))+"\\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");

				out.write("c(LO) & - & " + formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_cLO_hLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_cLO_hLOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_cLO_alphaLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_cLO_alphaLOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_cLO_accuracyLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_cLO_accuracyLOListArray))+"\\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");

				out.write("h(LO) & - & - &" + formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_hLO_alphaLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_hLO_alphaLOListArray))+ "& "+ formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_hLO_accuracyLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_hLO_accuracyLOListArray))+"\\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");

				out.write("A(LO) & - & - & - &" + formatter.format(WorkerUtilityClass.getAverage(metaCorrelationsManagersList.cor_alphaLO_accuracyLOListArray))+ " $\\pm$ " + formatterForErr.format(WorkerUtilityClass.findErrorBar(metaCorrelationsManagersList.cor_alphaLO_accuracyLOListArray))+"\\\\");
				out.write("\n");
				out.write("\\hline");
				out.write("\n");

				printLatexSynMetaCorrelationsForLOTableHeaderEndPartToTheFile(out, numberOfSharedElements);
				
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}

		public static void printLatexSynMetaCorrelationsForLOTableHeaderEndPartToTheFile(BufferedWriter out, int numberOfSharedElements)
		{
			try
			{
				out.write("\\end{tabular}");
				out.write("\n");			
				out.write("\\label{table:");				
				out.write("LinkOnlyMetaCorrelations_ms_"+ numberOfSharedElements +"}");
				out.write("\n");
				out.write("\\end{table}");
				out.write("\n");
				out.write("\n");
				out.write("\n");
				out.write("\n");
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}
